package com.ynkbny.web.controller;

import com.ynkbny.base.PageResult;
import com.ynkbny.domain.Enterprise;
import com.ynkbny.domain.dto.EnterpriseDTO;
import com.ynkbny.domain.pojo.EnterpriseAddVO;
import com.ynkbny.domain.pojo.EnterpriseGetNameVO;
import com.ynkbny.domain.pojo.EnterprisePaginReqVO;
import com.ynkbny.service.EnterpriseService;
import com.ynkbny.service.impl.EnterpriseServiceImpl;
import com.ynkbny.util.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 企业基本信息表-控制层
 *
 * @author : juzipi123
 * @date : 2023-7-5
 */
@Slf4j
@Api(tags = "企业基本信息表对象功能接口")
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 通过ID查询单条数据
     *
     * @author : juzipi123
     * @param enterpriseId 主键
     * @return 实例对象
     */
    @ApiOperation("通过ID查询单条数据")
    @GetMapping("/getById")
    public Res<Enterprise> getById(String enterpriseId) {
        log.info("请求参数:{}",enterpriseId);
        Enterprise enterprise = enterpriseService.queryById(enterpriseId);
        log.info("返回值：{}",enterprise);
        return Res.ok(enterprise);
    }

    /**
     * 分页查询
     *
     * @author : juzipi123
     * @param enterprise 筛选条件
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("/paginQuery")
    public Res<PageResult<Enterprise>> paginQuery(EnterprisePaginReqVO enterprise) {
        log.info("请求参数：{}",enterprise);
        PageResult<Enterprise> enterprisePageResult = enterpriseService.paginQuery(enterprise);
        log.info("返回值：{}",enterprisePageResult);
        return Res.ok(enterprisePageResult);
    }

    /**
     * 上传营业照片
     *
     * @param picture
     * @param request
     * @return
     */
    @ApiOperation("上传营业图片")
    @PostMapping("/upload")
    public Res<String> upload(@RequestParam("picture") MultipartFile picture, HttpServletRequest request) {
        String loadPicture = enterpriseService.upLoadPicture(picture, request);
        return Res.ok(loadPicture);
    }

    /**
     * 根据图片地址删除指定位置图片
     *
     * @author : xx
     * @param imagePath 图片地址
     * @return 是否成功
     */
    @ApiOperation("通过图片地址删除图片")
    @DeleteMapping("/removeByPath")
    public Res<String> removeByImagePath(String imagePath) {
        return EnterpriseServiceImpl.deleteImage(imagePath) ? Res.ok() : Res.fail();
    }

    /**
     * 新增数据
     *
     * @author : juzipi123
     * @param addVO 实例对象
     * @return 是否成功
     */
    @ApiOperation("新增数据")
    @PostMapping("/save")
    public Res<String> save(@RequestBody EnterpriseAddVO addVO) {
        log.info("请求参数：{}",addVO);
        EnterpriseDTO enterpriseDTO = new EnterpriseDTO();
        BeanUtils.copyProperties(addVO, enterpriseDTO);

        return enterpriseService.save(enterpriseDTO) ? Res.ok() : Res.fail();
    }

    /**
     * 更新数据
     *
     * @author : juzipi123
     * @param enterprise 实例对象
     * @return 是否成功
     */
    @ApiOperation("更新数据")
    @PutMapping("/updateById")
    public Res<String> updateById(@RequestBody Enterprise enterprise) {
        log.info("请求参数：{}",enterprise);
        return enterpriseService.updateById(enterprise) ? Res.ok() : Res.fail();
    }

    /**
     * 通过主键删除数据
     *
     * @author : juzipi123
     * @param enterpriseId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除数据")
    @DeleteMapping("/removeById")
    public Res<String> removeById(String enterpriseId) {
        log.info("请求参数：{}",enterpriseId);
        return enterpriseService.removeById(enterpriseId) ? Res.ok() : Res.fail();
    }

    /**
     * 通过主键进行逻辑删除数据
     *
     * @author : juzipi123
     * @param enterpriseId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键进行逻辑删除数据")
    @DeleteMapping("/logicDelete")
    public Res<String> logicDeleteById(String enterpriseId) {
        log.info("请求参数：{}",enterpriseId);
        return enterpriseService.logicRemoveById(enterpriseId) ? Res.ok() : Res.fail();
    }

    /**
     * 批量逻辑删除
     *
     * @param ids
     * @return 是否成功
     */
    @ApiOperation("通过主键进行逻辑批量删除数据")
    @DeleteMapping("/logicDeleteBatch")
    public Res<String> logicDeleteBatchById(@RequestParam(required = false, value = "ids[]") List<Long> ids){
        return enterpriseService.logicRemoveBatchById(ids)? Res.ok() : Res.fail();
    }


    /**
     * 获取工作密钥
     *
     * @author : juzipi123
     * @param enterpriseId 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键获取工作密钥")
    @GetMapping("/getSecretKey")
    public Res<String> getSecretKey(String enterpriseId) {
        log.info("请求参数：{}",enterpriseId);
        return enterpriseService.getSecretKey(enterpriseId) ? Res.ok() : Res.fail();
    }

    /**
     * 省平台注册
     *
     * @author : juzipi123
     * @param enterpriseId 主键
     * @return 是否成功
     */
    @ApiOperation("省平台注册")
    @PostMapping("/register")
    public Res<String> register(String enterpriseId) {
        log.info("请求参数：{}",enterpriseId);
        return enterpriseService.register(enterpriseId) ? Res.ok() : Res.fail();
    }

    /**
     * 修改注册信息
     *
     * @author : juzipi123
     * @param enterpriseId 修改的参数
     * @return 是否成功
     */
    @ApiOperation("修改注册信息")
    @PostMapping("/updateByIdRegister")
    public Res<String> updateByIdRegister(String enterpriseId) {
        log.info("请求参数：{}",enterpriseId);
        return enterpriseService.updateByIdRegister(enterpriseId) ? Res.ok() : Res.fail();
    }

    /**
     * 手动获取token
     *
     * @author : juzipi123
     * @param enterpriseId 主键
     * @return 是否成功
     */
    @ApiOperation("手动获取token")
    @PostMapping("/getToken")
    public Res<String> getToken(String enterpriseId) {
        log.info("请求参数：{}",enterpriseId);
        String token = enterpriseService.getToken(enterpriseId);
        log.info("返回值：{}",token);
        return Res.ok(token);
    }

    /**
     * @Author zelinwang
     * @param pageNum
     * @param pageSize
     * @param conditionJson
     * @return
     */
    @ApiOperation("分页查询测试")
    @GetMapping("/pageList")
    public Res<PageResult<Enterprise>> pageList(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize,
                                                @RequestParam(name = "condition", required = false) String conditionJson) {
        log.info("请求参数：{},{},{}",pageNum,pageSize,conditionJson);
        PageResult<Enterprise> enterprisePageResult = enterpriseService.pageList(conditionJson, pageNum, pageSize);
        log.info("返回值：{}",enterprisePageResult);
        return Res.ok(enterprisePageResult);
    }

    /**
     * 查询企业名称byId
     *
     * @Author YY
     * @param id 社会统一信用代码
     * @return enterprise实体
     */
    @ApiOperation("查询企业名称byId")
    @GetMapping("/getNameById/{id}")
    public Res<EnterpriseGetNameVO> getEnterpriseName(@PathVariable String id) {
        log.info("请求参数：{}",id);
        EnterpriseGetNameVO enterprise = enterpriseService.getEnterpriseName(id);
        log.info("返回值：{}",enterprise);
        return Res.ok(enterprise);
    }

    /**
     * 查询企业名称列表
     *
     * @Author YY
     * @return list
     */
    @ApiOperation("查询企业名称列表")
    @GetMapping("/getNameList")
    public Res<List<EnterpriseGetNameVO>> getEnterpriseNameList() {
        List<EnterpriseGetNameVO> enterprise = enterpriseService.getNameList();
        log.info("返回值：{}",enterprise);
        return Res.ok(enterprise);
    }

    /**
     * * @Author zelinwang
     * @param pageNum
     * @param pageSize
     * @param conditionJson
     * @return
     */
    @ApiOperation("企业列表分页查询")
    @GetMapping("/enterNamePageList")
    public Res<PageResult<Enterprise>> enterNamePageList(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                         @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize,
                                                         @RequestParam(name = "condition", required = false) String conditionJson) {
        log.info("请求参数：{}",pageNum,pageSize,conditionJson);
        PageResult<Enterprise> enterprisePageResult = enterpriseService.enterNamePageList(conditionJson, pageNum, pageSize);
        log.info("返回值：{}",enterprisePageResult);
        return Res.ok(enterprisePageResult);
    }
}